package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.dto.AddressBookDto;
import com.liusand.dto.IdDto;
import com.liusand.service.AddressBookService;
import com.liusand.vo.AddressBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :AddressBookController
 * creationTime :2022/12/5 16:54
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("list")
    public Result list(){
        List<AddressBookVo> addressBookVoList = addressBookService.list();
        return Result.success(addressBookVoList);
    }

    @GetMapping("{id}")
    public Result findAddressBookById(@PathVariable String id){
        if(id == null){
            return Result.error("查询地址失败");
        }
        AddressBookVo addressBookVo = addressBookService.findAddressBookById(id);
        if(addressBookVo == null){
            return Result.error("查询地址失败");
        }
        return Result.success(addressBookVo);
    }

    @GetMapping("default")
    public Result findDefaultAddressBook(){
        AddressBookVo addressBookVo = addressBookService.findDefaultAddressBook();
        if(addressBookVo == null){
            return Result.error("查询默认地址失败");
        }
        return Result.success(addressBookVo);
    }

    @PutMapping("default")
    public Result modifyDefaultAddressBook(@RequestBody IdDto idDto){
        if(idDto == null || !StringUtils.hasText(idDto.getId())){
            return Result.error("修改默认地址失败");
        }
        boolean modifyResult = addressBookService.modifyDefaultAddressBook(idDto);
        if(!modifyResult){
            return Result.error("修改默认地址失败");
        }
        return Result.success("修改默认地址成功");
    }

    @PostMapping
    public Result addAddressBook(@RequestBody AddressBookDto addressBookDto){
        if(addressBookDto == null){
            return Result.error("添加地址失败");
        }
        boolean addResult = addressBookService.addAddressBook(addressBookDto);
        if(!addResult){
            return Result.error("添加地址失败");
        }
        return Result.success("添加地址成功");
    }

    @PutMapping
    public Result modifyAddressBook(@RequestBody AddressBookDto addressBookDto){
        if(addressBookDto == null){
            return Result.error("修改地址失败");
        }
        boolean modifyResult = addressBookService.modifyAddressBook(addressBookDto);
        if(!modifyResult){
            return Result.error("修改地址失败");
        }
        return Result.success("修改地址成功");
    }

    @DeleteMapping
    public Result removeAddressBooksByIds(@RequestParam("ids") String ids){
        if(!StringUtils.hasText(ids)){
            return Result.error("删除地址失败");
        }
        String[] split = ids.split(",");
        List<String> idList = new ArrayList<>(Arrays.asList(split));
        boolean removeResult = addressBookService.removeAddressBooksByIds(idList);
        if(!removeResult){
            return Result.error("删除地址失败");
        }
        return Result.success("删除地址成功");
    }
}
